$(function() {
	layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function(){
		 var laydate = layui.laydate //日期
		  ,laypage = layui.laypage //分页
		  ,layer = layui.layer //弹层
		  ,table = layui.table //表格
		  ,carousel = layui.carousel //轮播
		  ,upload = layui.upload //上传
		  ,element = layui.element //元素操作
		  ,slider = layui.slider //滑块
	var T =	table.render({
	    elem: '#batch'
	    ,height: 420
	    ,url: '/selectBatch' //数据接口
	    ,title: '批次表'
	    ,page: true //开启分页
	    ,limit:5
	    ,limits:[5,10,15,20]
	    ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
	    ,totalRow: true //开启合计行
	    ,response : {
            statusCode : 200// 重新规定成功的状态码为 200，table 组件默认为 0
        }
	    ,parseData : function(res) { // res 即为原始返回的数据
            return {
                "count" : res.tol,
                "code" : res.status, // 解析接口状态
                "msg" : res.message, // 解析提示文本
                "data" : res.list
                // 解析数据列表
            };
        }
	    ,cols: [[ //表头
	      {type: 'checkbox', fixed: 'left'}
	      ,{field: 'batchId', title: '批次ID',align:'center', width:180}
	      ,{field: 'batchName', title: '批次名字',align:'center', width: 300}
	      ,{fixed: 'right', title:'操作',align:'center', toolbar: '#barDemo3'}
	    ]]
	  });
		 
		 table.on('tool(batch)', function(obj){	
			 var data = obj.data;
			 console.log(data);
			var batchId=data.batchId;
			 if(obj.event=='del'){
				 layer.confirm('真的删除行吗',function(index) {
				 $.ajax({
					 url:"/deleteBatch",
             		dataType: 'json',
                     type: 'post',
                     data:{batchId:batchId},
                     success: function () {
                    	 T.reload();
                    	 obj.del();
						layer.close(index);
                     	layer.msg("删除成功");
                     },
                     error: function () {
                        layer.msg("删除失败");
                     }
				 })
				 })
			 }else if(obj.event=='edit'){
				 layer.open({
						type : 1,
						skin : 'layui-layer-rim', // 加上边框
						area : [ '520px', '300px' ], // 宽高
						content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
								+ '  <br>\n'
								+ '  <div class="layui-form-item">\n'
								+ '    <label class="layui-form-label">批次ID</label>\n'
								+ '    <div class="layui-input-block">\n'
								+ '      <input type="text"  id="batchId" lay-verify="title" autocomplete="off"  readonly disabled class="layui-input layui-disabled">\n'
								+ '    </div>\n'
								+ '  </div>\n'
								+ '  <div class="layui-form-item">\n'
								+ '    <label class="layui-form-label">批次名字</label>\n'
								+ '    <div class="layui-input-block">\n'
								+ '      <input type="text" id="batchName" lay-verify="title" autocomplete="off" placeholder="不能为空" class="layui-input">\n'
								+ '    </div>\n'
								+ '  </div>\n'
								
								+ '<div ><button lay-submit type="button" class="layui-btn" style="margin-left: 160px;" id="btn12">提交</button>\n'
								+ '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
								+ '</form>'
					});
				$("#batchId").val(data.batchId);
				$("#batchName").val(data.batchName);
				$("body").on("click","#btn12",function(){
					var batchId=$("#batchId").val();
					var batchName=$("#batchName").val();
					if(batchId!=null && batchName!=null && batchName!='' && batchId!=''){
						$.ajax({
							url:'/updateBatch',
							dataType:'json',
							type:'post',
							data:{
								batchId:batchId,
								batchName:batchName	
							},
							success:function(res){
								if(res.message=="修改成功"){
									T.reload();
									layer.closeAll();
									layer.msg("修改成功");
								}else{
									layer.msg("含有相同批次名,不能修改！");
								}
									
						
							},
							error:function(){
									layer.msg("系统错误11");
								
							}
						})
					}else{
						layer.msg("存在空值，请填写完整再提交")
					}
				})
				
				 
			 }
		 })
		 $("#add2").on("click",function(){
				layer.open({
					type : 1,
					skin : 'layui-layer-rim', // 加上边框
					area : [ '520px', '300px' ], // 宽高
					content : '<form class="layui-form layui-form-pane" action="" id="form1">\n'
							+ '  <br>\n'
							+ '  <div class="layui-form-item">\n'
							+ '    <label class="layui-form-label">批次ID</label>\n'
							+ '    <div class="layui-input-block">\n'
							+ '      <input type="text" id="batchId" lay-verify="title" autocomplete="off"   class="layui-input">\n'
							+ '    </div>\n'
							+ '  </div>\n'
							+ '  <div class="layui-form-item">\n'
							+ '    <label class="layui-form-label">批次名字</label>\n'
							+ '    <div class="layui-input-block">\n'
							+ '      <input type="text" id="batchName" lay-verify="title" autocomplete="off" placeholder="不能为空" class="layui-input">\n'
							+ '    </div>\n'
							+ '  </div>\n'
							
							+ '<div ><button lay-submit type="button" class="layui-btn" style="margin-left: 160px;" id="btn12">提交</button>\n'
							+ '<button type="reset" class="layui-btn layui-btn-primary">重置</button></div>\n'
							+ '</form>'
				});
			$("body").on("click","#btn12",function(){	
				var batchId=$("#batchId").val();
				var batchName=$("#batchName").val();
				if(batchId!=null && batchName!=null && batchId!='' && batchName!=''){
					$.ajax({
						url:'/insertBatch',
						dataType:'json',
						type:'post',
						data:{
							batchId:batchId,
							batchName:batchName	
						},
						success:function(res){
							if(res.message=="ok"){
								T.reload();
								layer.closeAll();
								layer.msg("插入成功");
							}else{
								layer.msg("含有相同批次名或相同批次ID！");
							}
								
					
						},
						error:function(){
								layer.msg("系统错误11");
							
						}
					})
				}else{
					layer.msg("存在空值，请填写完整再提交")
				}
				
			})	 
				 
			})	 
				 
		 
	})
})